package com.wang.blog.service.impl;

import com.wang.blog.dao.MeArticleDAO;
import com.wang.blog.dao.MeCommentDAO;
import com.wang.blog.model.MeArticle;
import com.wang.blog.model.MeComment;
import com.wang.blog.model.MeCommentExample;
import com.wang.blog.model.SysUser;
import com.wang.blog.service.ArticleService;
import com.wang.blog.service.CommentService;
import com.wang.blog.service.UserService;
import com.wang.blog.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author shimh
 * <p>
 * 2018年1月25日
 */
@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    MeCommentDAO meCommentDAO;

    @Autowired
    UserService userService;

    @Autowired
    ArticleService articleService;

    @Autowired
    MeArticleDAO meArticleDAO;

    @Override
    public List<MeComment> findAll() {
        return null;
    }

    @Override
    public MeComment getCommentById(Integer id) {
        return null;
    }

    @Override
    public Integer saveComment(MeComment comment) {
        return null;
    }

    @Override
    public void deleteCommentById(Integer id) {

    }

    @Override
    public List<MeComment> listCommentsByArticle(Integer id) {



        MeCommentExample meCommentExample=new MeCommentExample();
        MeCommentExample.Criteria criteria=meCommentExample.createCriteria();

        criteria.andArticleIdEqualTo(id);
        criteria.andLevelEqualTo(String.valueOf(0));

        List<MeComment> list=meCommentDAO.selectByExample(meCommentExample);
        List<MeComment> re=new ArrayList<>();
        for (MeComment x:list
             ) {
            x.setAuthor(userService.getUserById(x.getAuthorId()));

            meCommentExample.clear();
            criteria=meCommentExample.createCriteria();
            criteria.andArticleIdEqualTo(id);
            criteria.andLevelGreaterThan("0");
            criteria.andParentIdEqualTo(x.getId());

            List<MeComment> inlist=meCommentDAO.selectByExample(meCommentExample);
            for (MeComment xx:inlist
                 ) {
                xx.setAuthor(userService.getUserById(xx.getAuthorId()));
                if(xx.getLevel().equals("2"))
                    xx.setToUser(userService.getUserById(xx.getToUid()));
            }
            x.setChildrens(inlist);

            re.add(x);
        }
        return re;
    }

    @Override
    public MeComment saveCommentAndChangeCounts(MeComment comment) {
        int count=1;
        articleService.getArticleAndAddViews(comment.getArticle().getId());

        SysUser sysUser=UserUtils.getCurrentUser();
        comment.setAuthorId(sysUser.getId());
        comment.setArticleId(comment.getArticle().getId());
        comment.setCreateDate(LocalDateTime.now());

        //设置level
        if(null == comment.getParentId()){
            comment.setLevel("0");
        }else{

            if(null == comment.getToUser()){
                comment.setLevel("1");
            }else{
                comment.setLevel("2");
                comment.setToUid(comment.getToUser().getId());
            }
        }

        meCommentDAO.insertSelective(comment);

        MeArticle meArticle=new MeArticle();
        meArticle.setId(comment.getArticle().getId());
        meArticle.setCommentCounts(meArticleDAO.selectByPrimaryKey(comment.getArticle().getId()).getCommentCounts()+1);
        meArticleDAO.updateByPrimaryKeySelective(meArticle);

        MeComment re= meCommentDAO.selectByPrimaryKey(comment.getId());
        re.setAuthor(userService.getUserById(re.getAuthorId()));
        if(re.getLevel().equals("2"))
            re.setToUser(userService.getUserById(re.getToUid()));
        return re;
    }

    @Override
    public void deleteCommentByIdAndChangeCounts(Integer id) {

    }
}
